<?php

namespace App\Admin\Controllers;

use App\Models\Bank;
use App\Models\Card;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CardController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Card(), function (Grid $grid) {
            $grid->model()
                ->with(['user','bank'])->orderByDesc('id');
            $grid->column('id');
            $grid->column('user.tiktokno', trans('user.fields.tiktokno'))->display(function ($var){
                return $this->user_id."({$var})";
            });

            $grid->column('username');
            //$grid->column('phone');
            $grid->column('ifsc')->display(function ($var){
                if(empty($this->bank_id)){
                    return $var;
                }else{
                    switch ($this->bank_id){
                        case 1:
                            return 'USDT:TRC';
                            break;
                        case 2:
                            return 'USDT:ERC';
                            break;
                        default:
                            return $this->bank->name;
                            break;
                    }
                }
            });
            $grid->column('number');
            $grid->column('created_at');

            $grid->quickSearch(['user_id','user.tiktokno', 'username','number']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('user.id', trans('user.fields.id'));
                $filter->like('user.tiktokno', trans('user.fields.tiktokno'));
                $filter->like('username');
                $filter->like('number');
            });
            //$grid->disableFilterButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->showQuickEditButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Card(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('bank_id');
            $show->field('number');
            $show->field('username');
            $show->field('phone');
            $show->field('email');
            $show->field('ifsc');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Card(), function (Form $form) {

            //$form->text('user_id')->required();
            $form->text('number')->required();
            $form->text('username')->required();
            if($form->model()->bank_id == 0){
                $form->text('ifsc')->required();
            }else{
                $form->select('bank_id')->required()->options(self::banks());
            }
        });
    }

    public function banks(){
        $banks = Bank::getBank();
        $data = [];
        foreach ($banks as $bank){
            $data[$bank['id']] = $bank['name'];
        }
        return $data;
    }

}
